package com.northwind.serviceImpl;

import com.northwind.annotation.AddLog;
import com.northwind.dao.VideoMapper;
import com.northwind.entity.Video;
import com.northwind.entity.VideoExample;
import com.northwind.service.VideoService;
import com.northwind.util.AliyunOSSUtil;
//import com.northwind.util.VideoCoverUtil;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.net.URL;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

@Service

public class VidoeServiceImpl implements VideoService {
    @Resource
    VideoMapper videoMapper;
    @Resource
    HttpServletRequest request;

    @Override
    public HashMap<String, Object> queryAllPage(Integer page, Integer rows) {
        HashMap<String, Object> map = new HashMap<>();
        //前台接收的参数 page当前页 rows每页展示的数据量
        //后台返回 page当前页 records总条数 total总页数 rows数据集合
        map.put("page",page);
        //创建条件对象
        VideoExample videoExample = new VideoExample();
        //总条数
        int records = videoMapper.selectCountByExample(videoExample);
        map.put("records",records);
        //总页数=总条数/每页展示条数，除不尽+1
        int total = records % rows == 0 ?records/rows:records/rows+1;
        map.put("total",total);
        //分页查询数据  1.条件 2.分页对象
        List<Video> videos = videoMapper.selectByExampleAndRowBounds(videoExample, new RowBounds((page - 1) * rows, rows));
        map.put("rows",videos);
        return map;
    }
    @AddLog(value = "修改视频")
    @Override
    public void update(Video video) {
        videoMapper.updateByPrimaryKeySelective(video);

    }

    @AddLog(value = "添加视频")
    @Override
    public String add(Video video) {
        video.setId(UUID.randomUUID().toString());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
        Date date = new Date();
        String nowTime = simpleDateFormat.format(date);
        try {
            Date nowTimeDate = simpleDateFormat.parse(nowTime);
            video.setUploadTime(nowTimeDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return video.getId();
    }

    @AddLog(value = "删除视频")
    @Override
    public void delete(Video video) {
        //https://videochannel.oss-cn-beijing.aliyuncs.com/video/1640247193741-453910553-1-208.mp4
        Video video1 = videoMapper.selectByPrimaryKey(video.getId());
        System.out.println("要删除的文件为:"+video1.getVideoPath());
        String[] split = video1.getVideoPath().split("/");
        //https:    videochannel.oss-cn-beijing.aliyuncs.com    headImg    640161262960-%24%25IB%40T1FPFTKH5E%60F2W3V6N.jpg
        String objectName = split[3] + "/" +split[4];
        System.out.println("映射到OSS的文件名:"+objectName);
        AliyunOSSUtil.deleteFileByAliyun("videochannel",objectName);
        videoMapper.deleteByPrimaryKey(video.getId());
    }

    @Override
    public HashMap<String, Object> fileUploadByAliyun(MultipartFile videoPath, String id) {
        HashMap<String, Object> hashMap = new HashMap<>();
        try {
            //获取文件名
            String originalFilename = videoPath.getOriginalFilename();
            String newFilename = System.currentTimeMillis()+"-"+originalFilename;
            String objectName = "video/"+newFilename;




            //上传视频至oss
            AliyunOSSUtil.uploadByteFileAliyun("videochannel",objectName,videoPath);




            //根据aliyun视频截取封面
            URL videoURL = AliyunOSSUtil.videoCoverByAliyun("videochannel", objectName);
            String[] split = newFilename.split("\\.");
            //拼接的封面名
            String coverName = split[0]+".jpg";
            //将网络图片上传
            AliyunOSSUtil.internetUploadByAliyun("videochannel","cover/"+coverName,videoURL.toString());



            /*//上传封面  参数:视频路径 封面保存路径
            String realPath = request.getSession().getServletContext().getRealPath("/upload/cover");
            System.out.println("realPath:"+realPath);
            File file = new File(realPath);
            if (!file.exists()){
                file.mkdirs();
            }
            String[] split = newFilename.split("\\.");
            //拼接的封面名
            String coverName = split[0]+".jpg";
            String coverPath = realPath + "/" + coverName;
            System.out.println("封面路径:"+coverPath);
            VideoCoverUtil.fetchFrame("https://videochannel.oss-cn-beijing.aliyuncs.com/"+objectName,coverPath);
            //封面上传至阿里云
            AliyunOSSUtil.localUploadAliyun("videochannel","cover/"+coverName,coverPath);
            //删除本地封面
            File files = new File(realPath,coverName);
            if(files.exists() && files.isFile()){
                files.delete();
                System.out.println("本地文件已删除");
            }*/





            //修改文件信息
            Video video = new Video();
            video.setId(id);
            System.out.println("要修改的VideoId:"+id);
            System.out.println("数据库headImg对应oss路径:https://videochannel.oss-cn-beijing.aliyuncs.com/"+objectName);
            video.setVideoPath("https://videochannel.oss-cn-beijing.aliyuncs.com/"+objectName);
            //https://videochannel.oss-cn-beijing.aliyuncs.com/headImg/1640161262960-%24%25IB%40T1FPFTKH5E%60F2W3V6N.jpg
            video.setCoverPath("https://videochannel.oss-cn-beijing.aliyuncs.com/cover"+coverName);
            videoMapper.updateByPrimaryKeySelective(video);
            hashMap.put("message","文件上传成功");
        } catch (Exception e) {
            e.printStackTrace();
            hashMap.put("message","文件上传失败");
        }
        return hashMap;
    }


}
